const g = getApp().globalData
const util = g.util

Page({
    data: {
        tabSelected: [true, false], //三个tab的显隐控制属性
        swiperCurrent: 0,
        swiperHeight: 0,//滑块高度

        managerList: [],
        managerBottomHidden: true,
        managerGifShow: true,
        managerHasData: false,
        managerPage: 1,
        managerPageSize: 10,

        ownerList: [],
        ownerBottomHidden: true,
        ownerGifShow: true,
        ownerHasData: false,
        ownerPage: 1,
        ownerPageSize: 10,
    },

    onLoad() {
        this.getWindowHeight()
        this.getManagerList()
        this.getOwnerList()
    },

    onReachBottom() {
        if (this.data.swiperCurrent == 0) {
            // 管理员
            if (this.managerRequestBack) {
                this.setData({
                    managerBottomHidden: false,
                    managerGifShow: this.data.managerHasData,
                })
                if (this.data.managerHasData) {
                    this.getManagerList()
                }
            }

        } else if (this.data.swiperCurrent == 1) {
            // 业主
            if (this.ownerRequestBack) {
                this.setData({
                    ownerBottomHidden: false,
                    ownerGifShow: this.data.ownerHasData,
                })
                if (this.data.ownerHasData) {
                    this.getOwnerList()
                }
            }
        }
    },

    //获取管理员列表
    getManagerList() {

        let url = `users/getAdminUser?page=${this.data.managerPage}&page_size=${this.data.managerPageSize}&role_type=2`
        this.managerRequestBack = false
        util.httpGet({
            url,
            header: { authorization: g.userInfo.token },
            fn: res => {
                let currList = this.data.managerPage == 1 ? [] : this.data.managerList
                let resList = res.data.result.rows
                let managerList = currList.concat(resList)
                this.setData({
                    managerList,
                    managerPage: ++this.data.managerList,
                    managerBottomHidden: true,
                    managerHasData: managerList.length != res.data.result.total,
                })
            },
            comFn: res => {
                this.managerRequestBack = true
            }
        })
    },

    //获取业主列表
    getOwnerList() {

        let url = `users/getAdminUser?page=${this.data.ownerPage}&page_size=${this.data.ownerPageSize}&role_type=1`
        this.ownerRequestBack = false
        util.httpGet({
            url,
            header: { authorization: g.userInfo.token },
            fn: res => {
                let currList = this.data.ownerPage == 1 ? [] : this.data.ownerList
                let resList = res.data.result.rows
                let ownerList = currList.concat(resList)
                this.setData({
                    ownerList,
                    ownerPage: ++this.data.ownerList,
                    ownerBottomHidden: true,
                    ownerHasData: ownerList.length != res.data.result.total,
                })
            },
            comFn: res => {
                this.ownerRequestBack = true
            }
        })
    },
    chooseTab: function (e) {
        //选择选项事件
        var index = e.currentTarget.dataset.index
        this.istabtap = true
        this.changeTab(index)
    },
    changeTab: function (index) {
        for (var i in this.data.tabSelected) {
            this.data.tabSelected[i] = (i == index)
        }
        this.setData({
            tabSelected: this.data.tabSelected,
            swiperCurrent: index
        })
    },
    swiperchange: function (e) {
        if (this.istabtap) {
            this.istabtap = false
        }
        else {
            var index = e.detail.current
            this.changeTab(index)
        }
    },

    getWindowHeight: function () {
        let systemInfo = wx.getSystemInfoSync()
        let swiperHeight = 0
        swiperHeight = Math.ceil(systemInfo.windowHeight * g.px2rpx - 80)
        this.setData({
            swiperHeight: swiperHeight,
        })
    },


})